package com.bw.controller;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.bw.config.Result;
import com.bw.domain.SysMenu;
import com.bw.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 王钰--WY
 * @create 2023-08-18 9:53
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    SysMenuService sysMenuService;
    @GetMapping("/menuList")
    public Result menuList(){
        List<SysMenu> list = sysMenuService.list();
        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        //转换器 (含义:找出父节点为字符串零的所有子节点, 并递归查找对应的子节点, 深度最多为 3)
        List<Tree<String>> treeNodes = TreeUtil.build(list, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getMenuId()+"");
                    tree.setParentId(treeNode.getParentId()+"");
                    tree.setWeight(treeNode.getOrderNum());
                    tree.setName(treeNode.getMenuName());
                    // 扩展属性 ...
                    tree.putExtra("path", treeNode.getPath());
                    tree.putExtra("component", treeNode.getComponent());
                    tree.putExtra("icon",treeNode.getIcon());
                });
        return Result.ok(treeNodes);
    }
}
